<!-- 基础设置 -> 账套设置 -> （弹窗组建） -->
<template>
    <div class="modal-wrapper">
        <a-modal :title="title" :visible="visible" :confirm-loading="confirmLoading" :okText="type <= 2 ? '下一步并保存' : '确定'" :cancelText="type > 1 ? '上一步' : '关闭'" @ok="handleOk" @cancel="handleCancel" :closable="false" :width="1000">
            <accountSet v-show="type == 1" ref="accountSetRef" @ok="next"></accountSet>
            <accountingSet v-show="type == 2" ref="accountingSetRef" @accountingSetok="next"></accountingSet>
            <!-- <enableSet v-show="type == 3" ref="enableSetRef" @enableSetok="next"></enableSet> -->
            <codingSet v-show="type == 4" ref="codingSetRef" @codingSetok="next"></codingSet>
        </a-modal>
    </div>
</template>

<script>
import accountSet from "../accountSetModal/accountSet"; // 账套管理
import accountingSet from "../accountSetModal/accountingSet"; // 会计期间
import enableSet from "../accountSetModal/enableSet"; // 账户启用
import codingSet from "../accountSetModal/codingSet"; // 编码规则

import { list as listAccount } from "@/api/erp/account";

export default {
    props: {
        array: {
            type: Array,
            default () {
                return [];
            },
        }
    },
    data () {
        return {
            currentAccount: {},
            visible: false,
            type: 1,
            title: "账套管理设置"
        };
    },
    methods: {
        showModal (visible) {
            this.visible = visible;
            if (this.type === 4) {
                this.type = 1;
            }
        },
        // 获取 第一个账套信息
        GetFirstAccount () {
            const { accountMap, year } = this;
            listAccount().then(response => {
                if (response.code === 200) {
                    const { data } = response;
                    this.currentAccount = data[0] || {};
                    this.$refs.accountSetRef.postData = this.currentAccount;
                    this.$refs.accountingSetRef.currentAccount = this.currentAccount;
                    this.$refs.accountingSetRef.edit();
                }
            }).catch(error => { });
        },
        handleOk (e) {
            //触发子组建保存账套管理设置
            const { type } = this;
            switch (type) {
                case 1: this.$refs.accountSetRef.save(); break;
                case 2: this.$refs.accountingSetRef.handleSubmit(); break;
                case 3: this.next(4); break;
                case 4: this.$refs.codingSetRef.saveRule(); break;
            }
        },
        handleCancel () {
            if (this.type > 1 && this.type <= 4) {
                this.type--;
                this.title = this.array[this.type - 1].title;
            } else {
                this.visible = false; // 关闭弹窗
                this.$emit("ok");
            }
        },
        recovery () {
            //复原 重第一步开始
            this.type = 1;
            this.visible = false;
            this.title = this.array[this.type - 1].title;
        },
        next (type) {
            if (type > 4 || type < 1) {
                this.visible = false;
                this.$emit("ok");
            }
            let arrIndex = type - 1;
            let item = this.array[arrIndex];
            console.log("跳转下一步：", type, this.array, arrIndex, item);
            //跳转第二步
            this.type = type;
            this.title = item.title;
            switch (type) {
                case 2: this.GetFirstAccount(); break;
                case 3:
                    this.$refs.enableSetRef.currentAccount = this.currentAccount;
                    this.$refs.enableSetRef.list();
                    break;
                case 4:
                    this.$refs.codingSetRef.currentAccount = this.currentAccount;
                    this.$refs.codingSetRef.loadRuleList();
                    break;
            }


        }
    },
    components: {
        accountSet,
        accountingSet,
        enableSet,
        codingSet
    }
};
</script>
<style lang="less" scoped>
    .modal-wrapper {
        height: auto;
    }
</style>